﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MF.BO.Objects;
using MF.DAL.EF;
using System.Data.Linq;
using System.Data.Entity;

namespace MF.DAL.Core
{
    public static partial class DALUtils
    {
        public static class TransactionDB
        {
            public static List<TransactionItem> GetAllTransaction()
            {
                List<TransactionItem> toReturn = new List<TransactionItem>();

                try
                {
                    using (myFinanceEntities ctx = new myFinanceEntities())
                    {
                        toReturn = (from row in ctx.Transaction
                                    select new TransactionItem
                                    {
                                        ID = row.Id,
                                        Name = row.Name
                                    }).ToList();
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                return toReturn;
            }

            public static int Insert(TransactionItem toInsert)
            {
                int toReturn = 0;

                try
                {
                    Transaction dbObject = new Transaction
                    {
                        Id = toInsert.ID,
                        Name = toInsert.Name,
                    };

                    using (myFinanceEntities ctx = new myFinanceEntities())
                    {
                        ctx.AddToTransaction(dbObject);
                        toReturn = ctx.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                return toReturn;
            }

            public static int Update(TransactionItem toUpdate)
            {
                int toReturn = 0;

                try
                {
                    using (myFinanceEntities ctx = new myFinanceEntities())
                    {
                        Transaction dbObject = (from row in ctx.Transaction where row.Id == toUpdate.ID select row).FirstOrDefault();

                        if (dbObject != null)
                        {
                            dbObject.Id = toUpdate.ID;
                            dbObject.Name = toUpdate.Name;
                        }

                        toReturn = ctx.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                return toReturn;
            }

            public static int Delete(int toDelete)
            {
                int toReturn = 0;

                try
                {
                    using (myFinanceEntities ctx = new myFinanceEntities())
                    {
                        Transaction dbObject = (from row in ctx.Transaction where row.Id == toDelete select row).FirstOrDefault();

                        if (dbObject != null)
                        {
                            ctx.DeleteObject(dbObject);
                        }

                        toReturn = ctx.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                return toReturn;
            }

        }
    }
}
